﻿using IHISCETeim.BLL;
using IHISCETeim.Model;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using IHISCETeim.Library;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;

namespace IHISCETeim.Web.AgentSys.VipMgr
{
    public partial class VipBuyAdd : AgentPageBase
    {
        protected override Permission[] PowerKey
        {
            get
            {
                return new Permission[] { Permission.VIPCustomer_AddBuy };
            }
        }
        protected override string DWZRef
        {
            get { return "VIPCustomerList"; }

        }

       
        protected int CustomerId
        {
            get
            {
                return GetRequestValue<int>("CustomerId", 0);
            }
        }
        private VIPCustomerInfo _vipObj = null;
        protected VIPCustomerInfo vipObj
        {
            get
            {
                if (_vipObj == null)
                    _vipObj = VIPCustomerController.GetList(CustomerId);

                return _vipObj;
            }
        }

        
        protected void Page_Load(object sender, EventArgs e)
        {
            if (GetRequestValue<string>("ajaxmethod", "") == "add")
            {
                add();//
            }
        }

        private void add()
        {
            VIPBuyInfo obj = Request.SerializableObject<VIPBuyInfo>();
            obj.UserId = vipObj.UserId;
            obj.AgentName = vipObj.AgentName;
            obj.AgentTotalPoint = obj.TotalPoint;


            char[] caSplit = new char[] { ',' };
            string[] ItemCodes = GetRequestValue<string>("ItemCode", "").Split(caSplit);
            string[] ItemNames = GetRequestValue<string>("ItemName", "").Split(caSplit);
            string[] Prices = GetRequestValue<string>("Price", "").Split(caSplit);
            string[] Quantitys = GetRequestValue<string>("Quantity", "").Split(caSplit);
            string[] DiscPrcnts = GetRequestValue<string>("DiscPrcnt", "").Split(caSplit);
            string[] LineTotals = GetRequestValue<string>("LineTotal", "").Split(caSplit);
            string[] Points = GetRequestValue<string>("Point", "").Split(caSplit);
           
            List<VIPBuyDetailInfo> list=new List<VIPBuyDetailInfo>();
            for (int rowIndex = 0; rowIndex < ItemCodes.Count(); rowIndex++)
            {
                VIPBuyDetailInfo itm = new VIPBuyDetailInfo();
                itm.ItemCode = ItemCodes[rowIndex];
                itm.ItemName = ItemNames[rowIndex];
                itm.Price =Convert.ToDecimal( Prices[rowIndex]);
                itm.Quantity =Convert.ToDecimal( Quantitys[rowIndex]);
                itm.DiscPrcnt =Convert.ToDecimal( DiscPrcnts[rowIndex]);
                itm.LineTotal =Convert.ToDecimal( LineTotals[rowIndex]);
                itm.Point =Convert.ToInt32( Points[rowIndex]);
                itm.IsDiscPiece=false;
                itm.AgentPoint=itm.Point;
                list.Add(itm);
            }


            int R = VIPBuyController.Add(obj, list,base.User.UserType==2);
            switch (R)
            {
                case 1:
                    SuccessJson("已成功添加VIP配件采购信息,同时邮件通知IHISCE配件部审核相应的积分.");
                    break;
                case 2:
                    SuccessJson("已成功添加VIP配件采购信息");
                    break;
                case 0:
                    ErrorJson("添加VIP配件采购信息失败");
                    break;
            }
        }
    }
}